package com.xiang.demo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

// 计算器
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "CalculatorActivity";

    TextView showTxt; // 显示
    TextView cleanTxt; // 清楚
    TextView dyTxt; //  =
    TextView addTxt;  //  +
    TextView subtxt; // -
    TextView mulTxt; // *
    TextView divTxt; // /
    TextView number1Txt, number2Txt, number3Txt, number4Txt, number5Txt,
            number6Txt, number7Txt, number8Txt, number9Txt, number0Txt;


    private String currentTxt1 = "";
    private String currentTxt2 = "";
    private int type = 0; //  1 + 2 — 3  *  4 /
    private Hello hello;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        findView();
        hello = new Hello();
    }

    private void setCleanData() {
        type = 0;
        currentTxt1 = "";
        currentTxt2 = "";
        showTxt.setText("");
    }

    /**
     * 记录 输入
     *
     * @param num
     */
    private void setCurrentTxt(String num) {
        if (type == 0) {
            // 第一个参数
            currentTxt1 = currentTxt1 + num;
            showTxt.setText(currentTxt1);
            return;
        }
        currentTxt2 = currentTxt2 + num;
        showTxt.setText(currentTxt2);
    }


    /**
     * 触发等于
     *
     * @param type 运算
     * @param t1   参数1
     * @param t2   参数2
     */
    private void SubmissionDaat(int type, String t1, String t2) {
        if (type == 0) {
            return;
        }

        if (t1 == "") {
            return;
        }

        if (t2 == "") {
            return;
        }
        Log.d(TAG, "参数1：" + t1 + "   参数2：" + t2 + "   运算符号：" + type);
        float parameter1 = Float.parseFloat(t1);
        float parameter2 = Float.parseFloat(t2);

        String resultTxt = null;
        switch (type) {
            case 1:
                resultTxt = hello.add(parameter1, parameter2) + "";
                break;
            case 2:
                resultTxt = hello.sub(parameter1, parameter2) + "";
                break;
            case 3:
                resultTxt = hello.mul(parameter1, parameter2) + "";
                break;
            case 4:
                resultTxt = hello.div(parameter1, parameter2) + "";
                break;
        }
        showTxt.setText(resultTxt);

    }


    private void findView() {
        showTxt = findViewById(R.id.show_txt); // 显示
        cleanTxt = findViewById(R.id.clean_txt);
        dyTxt = findViewById(R.id.dy_txt);
        addTxt = findViewById(R.id.jh_txt);
        subtxt = findViewById(R.id.jfh_txt);
        mulTxt = findViewById(R.id.cfh_txt);
        divTxt = findViewById(R.id.ch_txt);
        number1Txt = findViewById(R.id.number1_txt);
        number2Txt = findViewById(R.id.number2_txt);
        number3Txt = findViewById(R.id.number3_txt);
        number4Txt = findViewById(R.id.number4_txt);
        number5Txt = findViewById(R.id.number5_txt);
        number6Txt = findViewById(R.id.number6_txt);
        number7Txt = findViewById(R.id.number7_txt);
        number8Txt = findViewById(R.id.number8_txt);
        number9Txt = findViewById(R.id.number9_txt);
        number0Txt = findViewById(R.id.number0_txt);
        cleanTxt.setOnClickListener(this);
        dyTxt.setOnClickListener(this);
        addTxt.setOnClickListener(this);
        subtxt.setOnClickListener(this);
        mulTxt.setOnClickListener(this);
        divTxt.setOnClickListener(this);
        number1Txt.setOnClickListener(this);
        number2Txt.setOnClickListener(this);
        number3Txt.setOnClickListener(this);
        number4Txt.setOnClickListener(this);
        number5Txt.setOnClickListener(this);
        number6Txt.setOnClickListener(this);
        number7Txt.setOnClickListener(this);
        number8Txt.setOnClickListener(this);
        number9Txt.setOnClickListener(this);
        number0Txt.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.jh_txt:
                type = 1;
                break;

            case R.id.jfh_txt:
                type = 2;
                break;
            case R.id.cfh_txt:
                type = 3;
                break;
            case R.id.ch_txt:
                type = 4;
                break;
            case R.id.dy_txt:
                SubmissionDaat(type, currentTxt1, currentTxt2);
                break;
            case R.id.clean_txt:
                setCleanData();
                break;
            case R.id.number1_txt:
                setCurrentTxt("1");
                break;
            case R.id.number2_txt:
                setCurrentTxt("2");

                break;
            case R.id.number3_txt:
                setCurrentTxt("3");

                break;
            case R.id.number4_txt:
                setCurrentTxt("4");

                break;
            case R.id.number5_txt:
                setCurrentTxt("5");

                break;
            case R.id.number6_txt:
                setCurrentTxt("6");

                break;
            case R.id.number7_txt:
                setCurrentTxt("7");

                break;
            case R.id.number8_txt:
                setCurrentTxt("8");

                break;
            case R.id.number9_txt:
                setCurrentTxt("9");

                break;
            case R.id.number0_txt:
                setCurrentTxt("0");
                break;

        }

    }

}
